#import <Foundation/Foundation.h>




#if defined(__cplusplus)
#define ARL_EXTERN extern "C"
#else
#define ARL_EXTERN extern
#endif



#pragma mark ----Common Tools----
//Keywindow
//#define KEY_WINDOW [UIApplication sharedApplication].windows.firstObject
#define APP_KEY_WINDOW  [[UIApplication sharedApplication]keyWindow]
#define APP_TOP_VIEW  [[UIApplication sharedApplication]keyWindow].rootViewController.view
#define APP_TOP_VC  [[UIApplication sharedApplication]keyWindow].rootViewController
#define APP_APPLICATION             [UIApplication sharedApplication]
#define APP_APPLICATION_DELEGATE    ((AppDelegate *)[UIApplication sharedApplication].delegate)
// 设置图片
#define ImageNamed(__imageName) [UIImage imageNamed:__imageName]
//  通知中心
#define NotificationCenter [NSNotificationCenter defaultCenter]
/// 应用名称
#define APP_NAME    ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"])
/// 应用版本号
#define APP_VERSION ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"])
/// 应用build
#define APP_BUILD   ([[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"])

#pragma mark ----Device----
//SafeArea(刘海屏布局)
#define Safe_Area ((IS_iOS11_VERSTION_LATER) ? APP_KEY_WINDOW.safeAreaInsets : UIEdgeInsetsZero)
// 类型相关
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)


#define iPadMargin (IS_IPAD ? 150 : 0)
#define IS_IPHONE_4_OR_LESS  (IS_IPHONE && SCREEN_MAX_LENGTH  < 568.0)
#define IS_IPHONE_5          (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6          (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P         (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
//#define IS_IPHONE_X          (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0)
#define IS_IPHONE_SR         (IS_IPHONE && SCREEN_MAX_LENGTH == 896.0)
#define IS_IPHONE_X \
({BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneX);})
#pragma mark ----Screen----
// 系统放大倍数
#define SCREEN_SCALE [[UIScreen mainScreen] scale]
//屏幕尺寸相关
#define APP_WIDTH        ([[UIScreen mainScreen] bounds].size.width)
#define APP_HEIGHT       ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_WIDTH        ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT       ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_BOUNDS       ([[UIScreen mainScreen] bounds])
#define APP_BOUNDS       SCREEN_BOUNDS

#define SCREEN_MAX_LENGTH (MAX(APP_WIDTH, APP_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(APP_WIDTH, APP_HEIGHT))

#define APP_SIZE_IN_PIXEL       [ARLDeviceUtils deviceSizeInPixels]
#define APP_HEIGHT_IN_PIXEL     APP_SIZE_IN_PIXEL.height
#define APP_WIDTH_IN_PIXEL      APP_SIZE_IN_PIXEL.width
#define SCREEN_SIZE_IN_PIXEL    APP_SIZE_IN_PIXEL
#define SCREEN_HEIGHT_IN_PIXEL   APP_HEIGHT_IN_PIXEL
#define SCREEN_WIDTH_IN_PIXEL    APP_WIDTH_IN_PIXEL

//根据宽度缩放比例
#define Width              (IS_IPAD ? ([[UIScreen mainScreen]bounds].size.width/768.0f) : ([[UIScreen mainScreen]bounds].size.width/414.0f))
//根据高度缩放比例
#define Height            (IS_IPAD ? ([[UIScreen mainScreen]bounds].size.width/1024.0f) : ([[UIScreen mainScreen]bounds].size.height/736.0f))
//使用iphone宽高尺寸
#define WidthIphone              [[UIScreen mainScreen]bounds].size.width / 414.0f
#define HeightIphone             [[UIScreen mainScreen]bounds].size.height / 736.0f


#pragma mark ----FILE PATH----
// AppCaches 文件夹路径
#define APP_CACHE_DIR  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
// App DocumentDirectory 文件夹路径
#define APP_DOC_DIR  [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
// DIR FORMAT 如果没有斜杠则加上斜杠
#define APP_DIR_FORMAT(DIR_PATH) if(![DIR_PATH hasSuffix:@"/"]){DIR_PATH=[DIR_PATH stringByAppendingString:@"/"];}



#pragma mark ----StatusBar NavBar----
// 导航条高度
#define APPLICATION_NAV_BAR_HEIGHT (44.0)
#define APPLICATION_TOP_BAR_HEIGHT (APPLICATION_STATUS_BAR_HEIGHT+APPLICATION_NAV_BAR_HEIGHT)
// tabBar高度
#define APPLICATION_TAB_BAR_HEIGHT ((IS_IPHONE_X || IS_IPHONE_SR)?(83.0f+0.0f):(49.0f+0.0f))
//状态栏
#define APPLICATION_STATUS_BAR_HEIGHT ((IS_IPHONE_X || IS_IPHONE_SR)?44.0:20.0)


#pragma mark ----Color----
#define ColorFromHexA(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]

#define ColorFromHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define RandomColor ColorFromRGB(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256))



#pragma mark ----Log----
#ifdef DEBUG
#define ARLLOG_FUN             NSLog(@"#####%s#####", __FUNCTION__);
#define ARLLOG(CON)            NSLog(@"%@",CON)
#define ARLLOGT(CON)           NSLog(@"Arlene-->%@",CON)
#define ARLLOGF(fmt, ...)      NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#define ARLDEALLOC_LOG        -(void)dealloc{NSLog(@"%@ is dealloc",NSStringFromClass([self class]));}
#define ARLDEALLOC_LOGF(PRE)        -(void)dealloc{NSLog(@"%@::::::%@ is dealloc",PRE,NSStringFromClass([self class]));}
#else
#define ARLLOG_FUN
#define ARLLOG(CON)
#define ARLLOGT(CON)
#define ARLLOGF(fmt, ...)
#define ARLDEALLOC_LOG
#define ARLDEALLOC_LOGF(PRE)
#endif



#pragma mark -----转换类型-----
#define DATA_FROM_STRING(__STR__)         [__STR__ dataUsingEncoding:NSUTF8StringEncoding]
#define STRING_FROM_DATA(__DATA__)        [[NSString alloc]initWithData:__DATA__ encoding:NSUTF8StringEncoding]

#define JSON_DIC_FROM_STRING(__STR__)   [ARLStringHelper dictionaryFromJSONString:__STR__]
#define JSON_ARRAY_FROM_STRING(__STR__)   [ARLStringHelper arrayFromJSONString:__STR__]

#define JSON_OBJ_FROM_STRING(__STR__)   [ARLStringHelper objectFromJSONString:__STR__]

#define JSON_STRING_FROM_DIC(__DIC__)   [ARLStringHelper stringFromDictionary:__DIC__]
#define JSON_STRING_FROM_ARRAY(__ARRAY__)   [ARLStringHelper stringFromArray:__ARRAY__]


#pragma mark ----Version----
#define DEVICE_IOS_VERSION [[UIDevice currentDevice].systemVersion floatValue]
#define CURRENT_OS_VERSION DEVICE_IOS_VERSION

#define IS_iOS11_VERSTION_LATER ([UIDevice currentDevice].systemVersion.floatValue >= 11.0)
#define IS_iOS12_VERSTION_LATER ([UIDevice currentDevice].systemVersion.floatValue >= 12.0)
#define IS_iOS13_VERSTION_LATER ([UIDevice currentDevice].systemVersion.floatValue >= 13.0)

#pragma mark ----UI----

#define ALShowAlert(TITLE,MSG) [[[UIAlertView alloc] initWithTitle:TITLE message:MSG delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show]

#pragma mark ----常用判断----
// 是否为空对象
#define ARLObjectIsNil(__object)  ((nil == __object) || [__object isKindOfClass:[NSNull class]])

// 字符串为空
#define ARLStringIsEmpty(__string) ((__string.length == 0) || ARLObjectIsNil(__string))

// 字符串不为空
#define ARLStringIsNotEmpty(__string)  (!ARLStringIsEmpty(__string))

// 数组为空
#define ARLArrayIsEmpty(__array) ((ARLObjectIsNil(__array)) || (__array.count==0))

#pragma mark ----ScrollView 适配iPhone X + iOS 11----
/// 适配iPhone X + iOS 11
#define ARLAdjustsScrollViewInsets_Never(__scrollView)\
do {\
_Pragma("clang diagnostic push")\
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"")\
if ([__scrollView respondsToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {\
NSMethodSignature *signature = [UIScrollView instanceMethodSignatureForSelector:@selector(setContentInsetAdjustmentBehavior:)];\
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];\
NSInteger argument = 2;\
invocation.target = __scrollView;\
invocation.selector = @selector(setContentInsetAdjustmentBehavior:);\
[invocation setArgument:&argument atIndex:2];\
[invocation retainArguments];\
[invocation invoke];\
}\
_Pragma("clang diagnostic pop")\
} while (0)
